package cn.da.shuai.cool.finance.controller;

import cn.da.shuai.cool.common.entity.R;
import cn.da.shuai.cool.finance.model.CoolCashFlowForecast;
import cn.da.shuai.cool.finance.model.vo.SearchCashFlowForecastVo;
import cn.da.shuai.cool.finance.service.ICoolCashFlowForecastService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 现金流预测表 前端控制器
 * </p>
 *
 * @author 大帅哥
 * @since 2023-05-01
 */
@Tag(name = "现金流预测管理")
@RestController
@RequestMapping("/finance/cash-flow-forecast")
@RequiredArgsConstructor
public class CoolCashFlowForecastController {

    private final ICoolCashFlowForecastService cashFlowForecastService;

    @Operation(summary = "分页查询现金流预测")
    @PostMapping("/page")
    public R<Page<CoolCashFlowForecast>> page(@RequestBody SearchCashFlowForecastVo vo) {
        LambdaQueryWrapper<CoolCashFlowForecast> wrapper = new LambdaQueryWrapper<CoolCashFlowForecast>()
                .ge(vo.getStartDate() != null, CoolCashFlowForecast::getForecastDate, vo.getStartDate())
                .le(vo.getEndDate() != null, CoolCashFlowForecast::getForecastDate, vo.getEndDate())
                .eq(vo.getScenario() != null, CoolCashFlowForecast::getScenario, vo.getScenario())
                .orderByDesc(CoolCashFlowForecast::getForecastDate);

        return R.ok(cashFlowForecastService.page(new Page<>(vo.getPageNum(), vo.getPageSize()), wrapper));
    }

    @Operation(summary = "获取现金流预测详情")
    @GetMapping("/{id}")
    public R<CoolCashFlowForecast> getById(@PathVariable Long id) {
        return R.ok(cashFlowForecastService.getById(id));
    }

    @Operation(summary = "新增现金流预测")
    @PostMapping
    public R<Boolean> add(@RequestBody CoolCashFlowForecast cashFlowForecast) {
        return R.ok(cashFlowForecastService.save(cashFlowForecast));
    }

    @Operation(summary = "修改现金流预测")
    @PutMapping
    public R<Boolean> update(@RequestBody CoolCashFlowForecast cashFlowForecast) {
        return R.ok(cashFlowForecastService.updateById(cashFlowForecast));
    }

    @Operation(summary = "删除现金流预测")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        return R.ok(cashFlowForecastService.removeById(id));
    }
}